# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# toolchain build order: win32-api-headers->msys2-runtime->binutils->gcc->binutils->msys2-runtime
# NOTE: libtool requires rebuilt with each new gcc version

pkgbase=gcc
pkgname=('gcc' 'gcc-libs' 'gcc-fortran')
pkgver=9.1.0
pkgrel=2
pkgdesc="The GNU Compiler Collection"
arch=('i686' 'x86_64')
groups=('msys2-devel')
license=('GPL' 'LGPL' 'FDL' 'custom')
url="https://gcc.gnu.org/"
makedepends=('binutils' 'mpc-devel' 'gmp-devel'
             'mpfr-devel' 'isl-devel' 'zlib-devel'
             'windows-default-manifest')
STRIP_OPTION="strip"
#STRIP_OPTION="!strip"
#DEBUG_OPTION="debug"
DEBUG_OPTION="!debug"
options=('!emptydirs' "$STRIP_OPTION" "$DEBUG_OPTION")
source=(https://ftp.gnu.org/gnu/gcc/gcc-${pkgver}/gcc-${pkgver}.tar.gz
        0009-Cygwin-uses-sysv-ABI-on-x86_64-V2.patch
        0010-Do-not-version-lto-plugin-on-cygwin-mingw.patch
        0011-add-dummy-pthread-tsaware-and-large-address-aware-fo.patch
        0012-handle-dllimport-properly-in-medium-model-V2.patch
        0013-skip-test-for-cygwin-mingw.patch
        0014-64bit-Cygwin-uses-SEH.patch
        0015-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENABLE-for.patch
        0016-fix-some-implicit-declaration-warnings.patch
        0017-__cxa-atexit-for-Cygwin.patch
        0018-prevent-modules-from-being-unloaded-before-their-dto.patch
        0020-cygwin-uses-cyg-lib-prefix-v3.patch
        0022-libgomp-soname-cygwin-mingw.patch
        0024-libitm-weak-symbols.patch
        0028-g++-time.patch
        0030-newlib-ftm.patch
        0031-define_std-unix.patch
        0032-fix-ada-malloc-error.patch
        0130-libstdc++-in-out.patch
        0950-9.1.0-configure-msys2.patch
        0951-9.1.0-msys2-spec.patch
        0953-9.1.0-testsuite-msys2.patch)
sha256sums=('be303f7a8292982a35381489f5a9178603cbe9a4715ee4fa4a815d6bcd2b658d'
            '24bdd964c6479251af5a275965db42ed867599cd2d84ecb526397395e201cda4'
            '817361b4b301ca3f578d0d24b899691273a4ca9e20a91790a74efddc91c1130e'
            '7ad412357429a2e2376ec673d0e0a0d2ab4e3a162db532818bc76d55835f35d6'
            'cf33d6ff5e32ed35146117c9bd3d78bb9bf228abad73e5c2b44f006bf8cf0fc8'
            '4922110c9bb7959a453d62e0107aee7c58c1e146540f930f55466c72f24f72a7'
            '8227d3857cac3846d7436cd6867a7a89d36686d3219820215f72deb40ab44792'
            '84c8127f2e70f26a6bf0f7ff1fe06df4814096bc02f5fc19291a729c740887d7'
            '4d3030238181a6d28c1aff5e5b43db6d82517f3412309175e162f5da63f7cd00'
            '0e6a373a0911d81656d9bfd2d93085b817266538600eab14965f1bafb034bac8'
            'a277245cf7ccf6e3bd83a79945ff26ea5e41fb3ecf7fe0a473b2ec64d8c5b101'
            '0607c207a49ee337585f451cbdeb95131f45500361a494a88cfb304a4e18ace5'
            '00e528e198183799c00167cacc1a7b1cb9505ae93ef89e581632ffaae20080f2'
            'e8e4fa17a86b7c00426699173973faafdd585a84499a5acc95ee7701723ed82b'
            '041ec7903f3285d28a4c2949fbda7b830e9e32c7883de3649c53bf595df037a0'
            '081457942bab05b4583718dd9bef50cb42032c43a8ded6ad633d6993e4dd05c1'
            'ebd467d7cf1997d8feecb053ed83d93e199a39a0af3f01b2e343f8e23a654630'
            'd08df78b4c8cfaee819bea41ca61f0b69a9afd00605230541139d35baf9d6f1f'
            'a526cfab91a1499be92c824c7f2185a013a38dbdfbeabe63ecbe84451e07b021'
            'a77df7aed55abad81c607497571284d3fc5b1cb69befe5afdc9b0eee23597025'
            '259d025fec7b527a1044c798051e2d074088eca4b382eebff433d6deecabd282'
            '6f62bf968154323ef531716e086fe6132e5d3d7fc496e7159e3c59e436658bff')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/gcc-${pkgver}
  
  # Remove files created by patches
  rm -f config/mt-cygwin \
        gcc/ada/system-cygwin-x86_64.ads \
        libgomp/config/cygwin/plugin-suffix.h \
        libgcc/config/i386/t-msys \
        gcc/config/i386/msys.h \
        gcc/config/i386/msys-w64.h \
        gcc/config/i386/t-msys-w64 > /dev/null 2>&1 | true

  # Cygwin patches
  apply_patch_with_msg \
     0009-Cygwin-uses-sysv-ABI-on-x86_64-V2.patch \
     0010-Do-not-version-lto-plugin-on-cygwin-mingw.patch \
     0011-add-dummy-pthread-tsaware-and-large-address-aware-fo.patch \
     0012-handle-dllimport-properly-in-medium-model-V2.patch \
     0013-skip-test-for-cygwin-mingw.patch \
     0014-64bit-Cygwin-uses-SEH.patch \
     0015-define-RTS_CONTROL_ENABLE-and-DTR_CONTROL_ENABLE-for.patch \
     0016-fix-some-implicit-declaration-warnings.patch \
     0017-__cxa-atexit-for-Cygwin.patch \
     0018-prevent-modules-from-being-unloaded-before-their-dto.patch \
     0020-cygwin-uses-cyg-lib-prefix-v3.patch \
     0022-libgomp-soname-cygwin-mingw.patch \
     0024-libitm-weak-symbols.patch \
     0028-g++-time.patch \
     0030-newlib-ftm.patch \
     0031-define_std-unix.patch \
     0032-fix-ada-malloc-error.patch

  # MINGW Patches
  apply_patch_with_msg 0130-libstdc++-in-out.patch

  # MSYS2 Patches
  apply_patch_with_msg \
    0950-9.1.0-configure-msys2.patch \
    0951-9.1.0-msys2-spec.patch \
    0953-9.1.0-testsuite-msys2.patch
  
  # Do not run fixincludes
  #sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
  echo ${pkgver} > gcc/BASE-VER
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure
  
  [[ -d ${srcdir}/build-$(arch) ]] && rm -rf ${srcdir}/build-$(arch)
  mkdir ${srcdir}/build-$(arch)
}

build() {
  cd ${srcdir}/build-$(arch)

  case ${CARCH} in
    i686)
      _arch=i686
      _arch_conf="--disable-sjlj-exceptions"
    ;;
    x86_64)
      _arch=x86-64
      _arch_conf=
    ;;
  esac

  # libgcc is built with -g0, but INHIBIT_LIBC_CFLAGS can
  # be used to undo that.
  if check_option "debug" "y"; then
    export INHIBIT_LIBC_CFLAGS="-g2"
  fi

  # using -pipe causes spurious test-suite failures
  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48565
  CFLAGS=${CFLAGS/-pipe/}
  CXXFLAGS=${CXXFLAGS/-pipe/}
  
  # use built-in SSP with Cygwin 2.10
  # FIXME: --disable-libssp should suffice in GCC 8
  export gcc_cv_libc_provides_ssp=yes
  # libssp: conflicts with builtin SSP

  ${srcdir}/gcc-${pkgver}/configure \
    --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/lib \
    --enable-bootstrap \
    --enable-shared --enable-shared-libgcc --enable-static \
    --enable-version-specific-runtime-libs \
    --with-arch=${_arch} \
    --with-tune=generic \
    --disable-multilib \
    ${_arch_conf} \
    --enable-__cxa_atexit \
    --with-dwarf2 \
    --enable-languages=c,c++,fortran,lto \
    --enable-graphite \
    --enable-threads=posix \
    --enable-libatomic \
    --enable-libgomp \
    --enable-libitm \
    --enable-libquadmath \
    --enable-libquadmath-support \
    --disable-libssp \
    --disable-win32-registry \
    --disable-symvers \
    --with-gnu-ld \
    --with-gnu-as \
    --disable-isl-version-check \
    --enable-checking=release \
    --without-libiconv-prefix \
    --without-libintl-prefix \
    --with-system-zlib \
    --enable-linker-build-id \
    --with-default-libstdcxx-abi=gcc4-compatible \
    --enable-libstdcxx-filesystem-ts

  make

  # make documentation
  #cd $CHOST/libstdc++-v3
  #make doc-man-doxygen
}

#check() {
#  cd ${srcdir}/build-$(arch)

#  # do not abort on error as some are "expected"
#  make -k check || true
#  ${srcdir}/gcc-${pkgver}/contrib/test_summary
#}

package_gcc-libs()
{
  pkgdesc="Runtime libraries shipped by GCC"
  groups=('base')
  depends=('msys2-runtime')
  install=gcc-libs.install

  cd ${srcdir}/build-$(arch)
  make -j1 -C $CHOST/libgcc DESTDIR=${pkgdir} install-shared
  for lib in libgomp libstdc++-v3/src; do
    make -j1 -C $CHOST/$lib DESTDIR=${pkgdir} install-toolexeclibLTLIBRARIES
  done

  make -j1 -C $CHOST/libstdc++-v3/po DESTDIR=${pkgdir} install
  make -j1 -C $CHOST/libgomp DESTDIR=${pkgdir} install-info
  make -j1 -C $CHOST/libquadmath DESTDIR=${pkgdir} install-info

  make -j1 DESTDIR=${pkgdir} install-target-libquadmath
  make -j1 DESTDIR=${pkgdir} install-target-libgfortran
  #make -j1 DESTDIR=${pkgdir} install-target-libssp
  make -j1 DESTDIR=${pkgdir} install-target-libatomic
  #make -j1 DESTDIR=${pkgdir} install-target-libcilkrts
  make -j1 DESTDIR=${pkgdir} install-target-libvtv

  rm -f ${pkgdir}/*.dll
  rm -rf ${pkgdir}/usr/lib/
  rm -rf ${pkgdir}/usr/share/gcc-${pkgver}

  # remove static libraries
  #find ${pkgdir} -name *.a -delete

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc()
{
  pkgdesc="The GNU Compiler Collection - C and C++ frontends"
  depends=("gcc-libs=${pkgver}-${pkgrel}" 'binutils' 'gmp' 'isl' 'mpc' 'mpfr' 'msys2-runtime-devel' 'msys2-w32api-headers' 'msys2-w32api-runtime' 'windows-default-manifest')
  options=('staticlibs' "$STRIP_OPTION" "$DEBUG_OPTION")
  install=gcc.install

  cd ${srcdir}/build-$(arch)

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=${pkgdir} install
  if [ "$CARCH" = "x86_64" ]
  then
    mv ${pkgdir}/usr/lib/gcc/${CHOST}/lib/libgcc_s.dll.a ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver/-*/.*}/
    rm -rf ${pkgdir}/usr/lib/gcc/${CHOST}/lib
  fi
  # install -d ${pkgdir}/usr/share/gdb/auto-load/usr/lib
  # mv $pkg{pkgdir}dir{,/usr/share/gdb/auto-load}/usr/lib/libstdc++.so.6.0.18-gdb.py

  # unfortunately it is much, much easier to install the lot and clean-up the mess...
  rm ${pkgdir}/usr/bin/{$CHOST-,}gfortran
  # remove all DLLs
  rm -f ${pkgdir}/usr/bin/*.dll

  rm -f ${pkgdir}/usr/lib/libiberty.a
  # rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/libatomic{.dll,}.a
  rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/libgfortran{.dll,}.a
  rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/libgfortran.spec
  rm -r ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/finclude
  rm -f ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/f951.exe
  rm -f ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/{libcaf_single,libgfortranbegin}.a
  rm -f ${pkgdir}/usr/share/info/{gfortran,libgomp,libquadmath}.info
  rm -f ${pkgdir}/usr/share/locale/{de,fr}/LC_MESSAGES/libstdc++.mo
  rm -f ${pkgdir}/usr/share/man/man1/gfortran.1

  # remove static libraries - note libstdc++.a is needed for the binutils and glibc testsuite
  #rm ${pkgdir}/usr/lib/gcc/${CHOST}/${pkgver}/lib{gomp,quadmath}{.dll,}.a

  # many packages expect this symlinks
  ln -s gcc ${pkgdir}/usr/bin/cc
  cp -f ${pkgdir}/usr/bin/cpp.exe ${pkgdir}/usr/lib/cpp

  # POSIX conformance launcher scripts for c89 and c99
  cat > $pkgdir/usr/bin/c89 <<"EOF"
#!/bin/sh
fl="-std=c89"
for opt; do
  case "\$opt" in
    -ansi|-std=c89|-std=iso9899:1990) fl="";;
    -std=*) echo "`basename \$0` called with non ANSI/ISO C option \$opt" >&2
        exit 1;;
  esac
done
exec /usr/bin/gcc $fl ${1+"$@"}
EOF

  cat > ${pkgdir}/usr/bin/c99 <<"EOF"
#!/bin/sh
fl="-std=c99"
for opt; do
  case "\$opt" in
    -std=c99|-std=iso9899:1999) fl="";;
    -std=*) echo "`basename \$0` called with non ISO C99 option \$opt" >&2
        exit 1;;
  esac
done
exec /usr/bin/gcc $fl ${1+"$@"}
EOF

  chmod 755 ${pkgdir}/usr/bin/c{8,9}9

  # install the libstdc++ man pages
  # install -dm755 ${pkgdir}/usr/share/man/man3
  # install -m644 -t ${pkgdir}/usr/share/man/man3 \
  #  ${CHOST}/libstdc++-v3/doc/doxygen/man/man3/*.3

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc-fortran()
{
  pkgdesc="Fortran front-end for GCC"
  depends=("gcc=$pkgver-$pkgrel")
  options=('staticlibs' '!emptydirs' "$STRIP_OPTION" "$DEBUG_OPTION")
  install=gcc-fortran.install

  cd ${srcdir}/build-$(arch)

  if check_option "strip" "n"; then
    export STRIP=true
  fi

  make -j1 DESTDIR=${pkgdir} install-target-libgfortran
  make -j1 -C $CHOST/libgomp DESTDIR=${pkgdir} install-nodist_fincludeHEADERS
  make -j1 -C gcc DESTDIR=${pkgdir} fortran.install-{common,man,info}
  install -Dm755 gcc/f951 ${pkgdir}/usr/lib/gcc/$CHOST/$pkgver/f951

  ln -s gfortran ${pkgdir}/usr/bin/f95

  rm -f ${pkgdir}/usr/lib/gcc/${CHOST}/lib/libgcc_s.dll.a
  # remove files included in gcc-libs or gcc and unnneeded static lib
  rm ${pkgdir}/usr/bin/*.dll
  rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/libquadmath.{dll.a,a}
  rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/{*.o,libgc*}
  rm ${pkgdir}/usr/share/info/libquadmath.info
  rm -r ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/include
  # rm ${pkgdir}/usr/lib/gcc/$CHOST/${pkgver}/libgfortran.a

  # Install Runtime Library Exception
  install -Dm644 ${srcdir}/gcc-${pkgver}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-fortran/RUNTIME.LIBRARY.EXCEPTION
}
